import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../base/BaseController.dart';

class TestWordSpeedController extends BaseController {
  final String DATE_FORMAT = "%02d:%02d"; // 时间显示格式

  final num = 0.obs;
  final textNum = 0.obs;
  final time = 0.obs;

  final _timeInfo = "01:00".obs;

  String get timeInfo => _timeInfo.value;

  set timeInfo(String timeInfo) {
    _timeInfo.value = timeInfo;
  }

  TimerUtil? _timerUtil;

  TextEditingController _descController = TextEditingController();

  TextEditingController get descController => _descController;

  @override
  void onInit() {
    super.onInit();
    _descController.addListener(() {
      textNum.value = _descController.text.length;
    });
  }

  countDown() {
    //计时4秒
    int totalTime = 60 * 1000;
    // 初始化设置
    _timerUtil = new TimerUtil(mTotalTime: totalTime);
    _timerUtil?.setOnTimerTickCallback((int tick) {
      double _tick = tick / 1000;
      num.value = _tick.toInt();
      timeInfo =
          DateUtil.formatDateMs(tick.toInt(), format: "mm:ss").toString();
      if (num.value == 0) {
        _cancelTimer();
      }
    });
    _timerUtil?.startCountDown();
  }

  test() {
    _descController.text = '';
    textNum.value = 0;
    _cancelTimer();
    countDown();
  }

  @override
  void onClose() {
    super.onClose();
    _cancelTimer();
  }

  _cancelTimer() {
    if (_timerUtil != null && _timerUtil!.isActive()) {
      _timerUtil?.cancel();
    }
  }
}
